package objects;

import java.util.ArrayList;
import java.math.*;

import states.SlickGame;
import globals.FreeVector;

public abstract class Collidable {
	public int x,y,r; // x position, y position, collision radius
	public FreeVector direction;
	boolean hasCollided; 
	
	public Collidable(){ // blank constructor
		x=0;
		y=0;
		r=0;
		direction=new FreeVector();
		hasCollided=false;
	}
	public Collidable(int xin, int yin, int rin, FreeVector dir){ // standard constructor
		x=xin;
		y=yin;
		r=rin;
		direction=dir;
		hasCollided=false;
	}
	public Collidable(int xin, int yin, int rin){ // random vector constructor
		x=xin;
		y=yin;
		r=rin;
		direction=new FreeVector(5,(float)((2*Math.PI)*Math.random()));
		hasCollided=false;
	}
	
	public void update(ArrayList<Collidable> objects){
		// Check out of bounds.
		if(x<0){direction.add(new FreeVector(5,0));}
		else if(x>SlickGame.width){direction.add(new FreeVector(5,(float)(Math.PI)));}
		if(y<0){direction.add(new FreeVector(5,(float)(Math.PI/2)));}
		else if(y>SlickGame.height){direction.add(new FreeVector(5,(float)((3*Math.PI)/2)));}
		
		// check collision with other collidable objects.
		if(!hasCollided){
			for(int i=0; i<objects.size(); i++){
				if((getDistance(objects.get(i))<(this.r+objects.get(i).r))&&(objects.get(i)!=this)){ // if within colliding distance and not itself
					System.out.println("Collision Detected! "+i+">> "+objects.get(i).toString());
					this.hasCollided=true;
					objects.get(i).hasCollided=true;
					//objects.get(i).collide(this); // lets use the other objects collision for now.
					this.collide(objects.get(i));
				}
			}
		}
		
		// by direction to x/y
		hasCollided=false;
	}
	
	public abstract void collide(Collidable B);
	public float getDistance(Collidable B){ // get the distance between two collidable objects.
		return( (float)Math.sqrt(Math.pow((this.x-B.x),2)+Math.pow((this.y-B.y),2)));
	}
}
